/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * bus is the basic component of the kernel, which is used to manage the device and driver, if you 
 * register a bus, the bus-name directory will be created in '/', and the device and driver will be
 * created under the bus-name directory.
 * 
 * for example:
 * you register 'platform' bus, then the directory '/platform' will be created. then the /platform/devices
 * and /platform/drivers will be created.
 */

#include <kernel/bus.h>
#include <kernel/syslog.h>
#include <string.h>
#include <list.h>


/**
 * @brief Check whether the bus has been registered
 * @param bus: bus type
 * @return true: registered, otherwise: unregistered
 * @note bus name must be unique, if has been registered, return false
 */
bool bus_is_registered(struct bus_type *bus)
{
    if(kobj_find_by_name(NULL, bus->name) == NULL) {
        return false;
    }
    else {
        return true;
    }
}


/**
 * @brief Get bus name
 * @param bus: bus type
 * @return bus name, if not registered, return NULL
 * @note bus name must be unique
 */
const char *bus_get_name(struct bus_type *bus)
{
    if(bus && bus->kobj.name) {
        return bus->kobj.name;
    }

    return NULL;
}


/**
 * @brief Register bus type
 * @param bus: bus type
 * @return 0: success, otherwise: fail
 */
int bus_register(struct bus_type *bus)
{
    if(bus == NULL) {
        return -1;
    }

    if(bus->name == NULL) {
        SYSLOG_ERROR("bus's name is not set");
        return -1;
    }

    if(bus_is_registered(bus)) {
        SYSLOG_ERROR("%s bus is registered", bus->name);
        return -1;
    }

    kobj_init(&bus->kobj, bus->name, NULL);
    if(!kobj_add(NULL, &bus->kobj)){
        return -1;
    }

    kobj_init(&bus->dev_kobj, "devices", NULL);
    if(!kobj_add(&bus->kobj, &bus->dev_kobj)){
        return -1;
    }

    kobj_init(&bus->drv_kobj, "drivers", NULL);
    if(!kobj_add(&bus->kobj, &bus->drv_kobj)){
        return -1;
    }

    SYSLOG_INFO("%s bus register successfully", bus->name);
    return 0;
}


/**
 * @brief Unregister bus type
 * @param bus: bus type
 * @return none
 */
void bus_unregister(struct bus_type *bus)
{
    if(bus_is_registered(bus)) {
        kobj_remove_self(&bus->kobj);
        return;
    }

    SYSLOG_WARN("bus is not register, so it can not delete");
}


/**
 * @brief Find the index of the match table according to the compatible name
 * @param table: match table
 * @param compatible: compatible name
 * @return index of the match table, otherwise: -1
 * @note compatible name must be unique
 */
int bus_attach_match_table(struct of_device_id *table, const char *compatible)
{
    for(int i = 0; ; i++) {
        if(*table[i].compatible && compatible) {
            if(strcmp(table[i].compatible, compatible) == 0) {
                return i;
            }
        }
        else {
            return -1;
        }
    }

    return -1;
}


/**
 * @brief Find the index of the match table according to the device and driver name
 * @param dev_name: device name
 * @param drv_name: driver name
 * @return index of the match table, otherwise: -1
 * @note dev_name and drv_name should not be NULL
 */
int bus_attach_match_name(const char* dev_name, const char* drv_name)
{
    return strcmp(dev_name, drv_name) == 0 ? 0 : -1;
}


/**
 * @brief Simple match function
 * @param dev: device
 * @param drv: driver
 */
int bus_simple_match(struct device *dev, struct driver *drv)
{
    int ret = -1;

    if(drv->of_match_table) {
        ret = bus_attach_match_table(drv->of_match_table, dev->name);

        if(ret < 0) {
            ret = bus_attach_match_table(drv->of_match_table, dev->compatible);
        }
    }

    if(ret < 0) {
        if (bus_attach_match_name(dev->name, drv->name) == 0) {
            SYSLOG_INFO("%s bus: match successful: name = %s", drv->bus->name, drv->name);
            return 0;
        }
    }
    else {
        device_set_platform_data(dev, drv->of_match_table[ret].data);
        SYSLOG_INFO("%s bus: match successful: match table = [%d], %s", drv->bus->name, ret, drv->of_match_table[ret].compatible);
        return 0;
    }

    DEBUG_TRACE("%s bus: no matching driver available for %s", drv->bus->name, dev->name);
    return -1;
}
